package com.proton.carepatchtemp.database.table;


import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.BaseBean;
import com.proton.carepatchtemp.utils.net.pair.ParamPair;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Unique;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Api on 2016/9/27.
 */
@Entity
public class Profile extends BaseBean {
    @Id(autoincrement = false)
    @NotNull
    @Unique
    public Long id;
    public String title = "";
    @NotNull
    public String realname = "";
    @NotNull
    public int gender = 1;  //1是男，2是女
    @NotNull
    public String birthday = "";
    public String height = "";
    public String weight = "";
    public String avatar = "";  //头像路径
    public boolean isdefault = false;
    public long uid = VCare.get().getUid();
    public String hypersusceptibility = "";  //过敏反应
    public String history = "";  //病历
    public String memo = "";  //备注.
    public int menstrualphase = 0;  //月经天数
    public int menstrualcycle = 0;  //月经周期
    @Transient
    public List<String> sharelist = null;
    @Transient
    private Date mDate;

    @Generated(hash = 256038023)
    public Profile(@NotNull Long id, String title, @NotNull String realname,
                   int gender, @NotNull String birthday, String height, String weight,
                   String avatar, boolean isdefault, long uid, String hypersusceptibility,
                   String history, String memo, int menstrualphase, int menstrualcycle) {
        this.id = id;
        this.title = title;
        this.realname = realname;
        this.gender = gender;
        this.birthday = birthday;
        this.height = height;
        this.weight = weight;
        this.avatar = avatar;
        this.isdefault = isdefault;
        this.uid = uid;
        this.hypersusceptibility = hypersusceptibility;
        this.history = history;
        this.memo = memo;
        this.menstrualphase = menstrualphase;
        this.menstrualcycle = menstrualcycle;
    }

    @Generated(hash = 782787822)
    public Profile() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getAge() {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

        try {
            mDate = fmt.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return getAgeByBirthday(mDate);
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getHypersusceptibility() {
        return hypersusceptibility;
    }

    public void setHypersusceptibility(String hypersusceptibility) {
        this.hypersusceptibility = hypersusceptibility;
    }

    public String getHistory() {
        return history;
    }

    public void setHistory(String history) {
        this.history = history;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public List<ParamPair> toArray() {
        List<ParamPair> list = new ArrayList<>();
        list.add(new ParamPair(Extras.PRF_PROFILEID, id));
        list.add(new ParamPair(Extras.PRF_TITLE, title));
        list.add(new ParamPair(Extras.PRF_REALNAME, realname));
        list.add(new ParamPair(Extras.PRF_GENDER, gender));
        list.add(new ParamPair(Extras.PRF_BIRTHDAY, birthday));
        list.add(new ParamPair(Extras.PRF_HEIGHT, height));
        list.add(new ParamPair(Extras.PRF_WEIGHT, weight));
        list.add(new ParamPair(Extras.PRF_HISTORY, history));
        list.add(new ParamPair(Extras.PRF_HSP, hypersusceptibility));
        list.add(new ParamPair(Extras.PRF_MEMO, memo));
        list.add(new ParamPair(Extras.AVATAR, avatar));
        list.add(new ParamPair(Extras.PRF_PERIOD, menstrualphase));
        list.add(new ParamPair(Extras.PRF_MENSES, menstrualcycle));
        return list;
    }

    /**
     * 根据用户生日计算年龄
     */
    private String getAgeByBirthday(Date birthday) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthday)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(birthday);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                // monthNow==monthBirth
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                // monthNow>monthBirth
                age--;
            }
        }
        return String.valueOf(age);
    }

    public boolean getIsdefault() {
        return this.isdefault;
    }

    public void setIsdefault(boolean isdefault) {
        this.isdefault = isdefault;
    }

    public long getUid() {
        return this.uid;
    }

    public void setUid(long uid) {
        this.uid = uid;
    }

    public int getMenstrualphase() {
        return this.menstrualphase;
    }

    public void setMenstrualphase(int menstrualphase) {
        this.menstrualphase = menstrualphase;
    }

    public int getMenstrualcycle() {
        return this.menstrualcycle;
    }

    public void setMenstrualcycle(int menstrualcycle) {
        this.menstrualcycle = menstrualcycle;
    }

    public Date getMDate() {
        return this.mDate;
    }

    public void setMDate(Date mDate) {
        this.mDate = mDate;
    }
}
